package com.drinks.yiniang.ui.activity.user.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.user.address.DeliverAddressAdapter;
import com.drinks.yiniang.common.recycler.decoration.ItemSpacesDecoration;
import com.drinks.yiniang.entity.user.address.DeliverAddressJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnSureCancelListener;
import com.hansen.library.ui.widget.dialog.MaterialDialog;
import com.hansen.library.ui.widget.layout.BorderFrameLayout;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.hansen.library.Constants;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 收货地址列表页面
 *
 * @author han
 * @date 2019/09/06
 */
public class DeliverAddressActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener, OnSureCancelListener {

    private final int REQ_ADD_ADDRESS_CODE = 0x01;//添加地址
    private final int REQ_EDIT_ADDRESS_CODE = 0x02;//地址编辑

    //nav
    private NavigationBarLayout nav_bar_deliver_address;
    private BaseRecyclerView rcv_deliver_address;
    //添加地址
    private BorderFrameLayout fl_add_deliver_address;

    private DeliverAddressAdapter mAddressAdapter;

    //是否需要选择地址
    private boolean isChooseAddress;

    @Override
    protected void initView() {
        nav_bar_deliver_address = findViewById(R.id.nav_bar_deliver_address);

        rcv_deliver_address = findViewById(R.id.rcv_deliver_address);

        fl_add_deliver_address = findViewById(R.id.fl_add_deliver_address);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        if(getIntent() != null) {
            isChooseAddress = getIntent().getBooleanExtra(Constants.KeyChoose, false);
        }

        initAdapter();
        getAddressList();
    }

    private void initAdapter() {
        rcv_deliver_address.setLayoutManager(new LinearLayoutManager(mContext));
        rcv_deliver_address.addItemDecoration(ItemSpacesDecoration.getItemDPDecoration(10));

        mAddressAdapter = new DeliverAddressAdapter(mContext, new ArrayList<DeliverAddressJson.DeliverAddressData>());
        mAddressAdapter.setCenterEmpty(getLayoutInflater(), rcv_deliver_address);
        mAddressAdapter.bindToRecyclerView(rcv_deliver_address);
    }

    @Override
    protected void initListener() {
        nav_bar_deliver_address.setOnNavgationBarClickListener(this);
        fl_add_deliver_address.setOnClickListener(this);

        mAddressAdapter.setOnItemClickListener(this);
        mAddressAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.fl_add_deliver_address://添加地址
                Intent intent = new Intent(mContext, AddAddressActivity.class);
                startActivityForResult(intent, REQ_ADD_ADDRESS_CODE);
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_deliver_address;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.RESULT_CODE_NORMAL) {
            switch (requestCode) {
                case REQ_ADD_ADDRESS_CODE://添加
                case REQ_EDIT_ADDRESS_CODE://编辑
                    getAddressList();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        DeliverAddressJson.DeliverAddressData data = mAddressAdapter.getItem(position);

        if (data == null) {
            return;
        }

        switch (view.getId()) {
            case R.id.tv_deliver_address_edit://编辑
                Intent intent = new Intent(mContext, AddAddressActivity.class);
                intent.putExtra(Constants.KeyAddressId, data.getId());
                startActivityForResult(intent, REQ_EDIT_ADDRESS_CODE);
                break;
            case R.id.tv_deliver_address_delete://删除
                MaterialDialog dialog = MaterialDialog.newInstance(position, getString(R.string.text_delete_address_sure), data.getId(), true);
                dialog.show(getSupportFragmentManager(), Constants.TAG_TIPS_DIALOG);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (mAddressAdapter.getItem(position) == null) {
            return;
        }

        Intent mIntent;

        if(isChooseAddress) {
            mIntent = new Intent();
            mIntent.putExtra(Constants.KeyObject, mAddressAdapter.getItem(position));
            setResult(Constants.RESULT_CODE_NORMAL, mIntent);
            finish();
            return;
        }

        mIntent = new Intent(mContext, AddAddressActivity.class);
        mIntent.putExtra(Constants.KeyAddressId, mAddressAdapter.getItem(position).getId());
        startActivityForResult(mIntent, REQ_EDIT_ADDRESS_CODE);
    }

    @Override
    public void onSure(int type, String value) {
        deleteAddress(type, value);
    }

    @Override
    public void onCancel(int type) {

    }

    //删除收货地址
    private void deleteAddress(final int pos, final String addressId) {
        if (StringUtils.isEmpty(addressId)) {
            return;
        }

        showLoadingDialog();

        UserApiIO.getInstance().deleteDeliverAddress(addressId, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                DeliverAddressJson.DeliverAddressData data = mAddressAdapter.getItem(pos);

                if (data != null && addressId.equals(data.getId())) {
                    mAddressAdapter.remove(pos);
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //获取地址列表
    private void getAddressList() {
        showLoadingDialog();

        UserApiIO.getInstance().getDeliverAddressList(new APIRequestCallback<DeliverAddressJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(DeliverAddressJson res) {
                mAddressAdapter.setNewData(res.getData());
                mAddressAdapter.loadMoreComplete();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mAddressAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        isChooseAddress = false;
        super.onDestroy();
    }
}
